import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;

public class HttpFutureListener implements FutureListener<String> {

    private ChannelHandlerContext ctx;

    public HttpFutureListener(ChannelHandlerContext ctx){
       this.ctx = ctx;
    }

    @Override
    public void operationComplete(Future<String> future) throws Exception {
        String message = future.get();
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK,
                Unpooled.wrappedBuffer(message.getBytes(CharsetUtil.UTF_8)));
        //response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json;charset=utf-8");
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        //response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        ctx.channel().writeAndFlush(response);/*.addListener(ChannelFutureListener.CLOSE);*/
    }
}
